package com.aizuda.boot.modules.business.template.entity.vo;

import com.aizuda.boot.modules.business.contract.entity.vo.ContractFormDataDTO;
import com.aizuda.boot.modules.file.domain.vo.FileVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Schema(description = "合同模版响应")
public class ContractTemplateRespDTO {

    @Schema(description = "合同模版ID")
    private Integer templateId;

    @Schema(description = "模版名称")
    private String templateName;

    @Schema(description = "模板编号，格式：年月日+序号，如：202506300001")
    private String templateCode;

    @Schema(description = "适用范围类型：department、personnel")
    private String scopeType;

    @Schema(description = "适用范围值（JSON：部门ID或人员ID列表）")
    private String scopeValue;

    @Schema(description = "创建时是否可编辑：true 可修改，false 不可修改")
    private Boolean editableWhenCreating;

    @Schema(description = "使用说明")
    private String usageInstructions;

    @Schema(description = "合同模版文件URL")
    private String templateFileUrl;

    @Schema(description = "合同模版文件名称")
    private String templateFileName;

    @Schema(description = "合同模版文件信息")
    private FileVO fileInfo;

    @Schema(description = "合同模版文件ID")
    private Integer templateFileId;

    @Schema(description = "状态：draft 草稿，published 发布")
    private String status;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    @Schema(description = "一级合同类型")
    private Integer contractTypeLevel1;

    @Schema(description = "二级合同类型")
    private Integer contractTypeLevel2;

    @Schema(description = "三级合同类型")
    private Integer contractTypeLevel3;

    @Schema(description = "自定义字段列表")
    private List<CustomFieldRespDTO> customFields;

    @Schema(description = "默认字段列表")
    private List<DefaultFieldRespDTO> defaultFields;

    @Schema(description = "合同表单数据")
    private ContractFormDataDTO content;

    @Schema(description = "关联的流程ID")
    private Long processId;

    @Data
    @Schema(description = "自定义字段响应")
    public static class CustomFieldRespDTO {

        @Schema(description = "主键ID")
        private Long id;

        @Schema(description = "字段编码")
        private String fieldCode;

        @Schema(description = "字段名称")
        private String fieldName;

        @Schema(description = "字段类型（文本、数字、日期、下拉框、金额等）")
        private String fieldType;

        @Schema(description = "是否必填")
        private Boolean isRequired;

        @Schema(description = "是否支持对方填写")
        private Boolean allowCounterpartyFill;

        @Schema(description = "字段说明")
        private String fieldDescription;

        @Schema(description = "默认值")
        private String defaultValue;

        @Schema(description = "字段显示条件（JSON）")
        private String displayConditions;

        @Schema(description = "字段使用次数")
        private Integer usageCount;

        @Schema(description = "排序顺序")
        private Integer sortOrder;
    }

    @Data
    @Schema(description = "默认字段响应")
    public static class DefaultFieldRespDTO {

        @Schema(description = "主键ID")
        private Long id;

        @Schema(description = "默认字段ID（关联contract_field表）")
        private Long fieldId;

        @Schema(description = "字段名称（从contract_field表获取）")
        private String fieldName;

        @Schema(description = "字段类型（从contract_field表获取）")
        private String fieldType;

        @Schema(description = "字段描述（从contract_field表获取）")
        private String fieldDescription;

        @Schema(description = "是否必填（从工作流字段配置获取）")
        private Boolean isRequired;

        @Schema(description = "使用次数")
        private Integer usageCount;

        @Schema(description = "排序字段，值越小越靠前")
        private Integer sortOrder;

        @Schema(description = "是否支持默认值")
        private Boolean supportDefaultValue;

        @Schema(description = "默认值")
        private String defaultValue;

        @Schema(description = "是否支持对方填写")
        private Boolean supportCounterpartyInput;
    }

    @Data
    @Schema(description ="模版模块字段树")
    public static class TemplateModuleFieldTreeDTO {
        
        @Schema(description = "模块ID")
        private Integer moduleId;
        
        @Schema(description = "模块名称")
        private String moduleName;
        
        @Schema(description = "模块描述")
        private String moduleDescription;
        
        @Schema(description = "模块排序")
        private Integer moduleOrder;
        
        @Schema(description = "字段树")
        private List<TemplateFieldTreeDTO> fields;
    }

    @Data
    @Schema(description ="模版字段树节点")
    public static class TemplateFieldTreeDTO {
        
        @Schema(description = "字段ID")
        private Long fieldId;
        
        @Schema(description = "父级字段ID")
        private Long parentFieldId;
        
        @Schema(description = "字段名称")
        private String fieldName;
        
        @Schema(description = "字段类型")
        private String fieldType;
        
        @Schema(description = "字段选项（JSON格式存储）")
        private String fieldOptions;
        
        @Schema(description = "字段描述")
        private String fieldDescription;
        
        @Schema(description = "是否必填")
        private Boolean isRequired;
        
        @Schema(description = "是否启用")
        private Boolean isEnabled;
        
        @Schema(description = "是否系统字段")
        private Boolean isSystem;
        
        @Schema(description = "数字类型保留小数位数")
        private Integer decimalPlaces;
        
        @Schema(description = "数字类型单位")
        private String unit;
        
        @Schema(description = "字段排序")
        private Integer fieldOrder;
        
        @Schema(description = "是否隐藏")
        private Boolean isHidden;
        
        @Schema(description = "子字段列表")
        private List<TemplateFieldTreeDTO> children;
    }
} 